package cn.pconline.test;

import java.util.ArrayList;
import java.util.List;

import cn.pconline.autoclub.util.DateUtils;

public class Snippet {
	public static int fnType1(int n)throws Exception{
	  if(n==0){
	   return 0;
	  }else if(n==1||n==2){
	   return 1;
	  }else if(n>2){
	   int temp=fnType1(n-1)+fnType1(n-2);
	   if(temp<0){
	    throw new Exception("Invalid value for int type, too larage");
	   }else{
	    return temp;
	   }
	  }else{
	   throw new Exception("IllegalArgument value for n,please enter n>=0 ");
	  }
	 } 
	
	public static void main(String[] args) throws Exception {
		/*System.out.println("function1 start:"+DateUtils.getNow());
		int a = fnType1(45);
		System.out.println(a);
		System.out.println("function1 end:"+DateUtils.getNow());
		System.out.println("function2 start:"+DateUtils.getNow());
		int b = fnType2(45);
		System.out.println(b);
		System.out.println("function2 end:"+DateUtils.getNow());*/
		int c =getFnData(20);
		System.out.println(c);
	}
	
	public static int fnType2(int n){
		  int result=-1;
		  int temp1=0;
		  int temp2=1;
		  
		  for(int index=0;index<=n;index++){
			   if(index==0){
				   result=temp1;
			   }else if(index==1){
				   result=temp2;
			   }else{
				   result=temp1+temp2;
				    if(result<0){
				     result=-2;
				     break;
				    }
				    temp1=temp2;
				    temp2=result;
			   }
		 }
		  return result;
	}
	
	 private static List<Integer> fnData=new ArrayList<Integer>();
	 private static final int maxSize=50000;
	 private static  void setFnData(){
		  int result=-1;
		  int temp1=0;
		  int temp2=1;
		  for(int index=0;index<=maxSize;index++){
			   if(index==0){
				   result=temp1;
			   }else if(index==1){
				   result=temp2;
			   }else{
				    result=temp1+temp2;
				    if(result<0){
				     result=-2;
				     break;
			    }
			    temp1=temp2;
			    temp2=result;
			   }
		   fnData.add(result);
		  }
		 }
		 /**
		  * 对外接口
		  * @Title: getFnData
		  * @Description: TODO
		  * @param @param n
		  * @param @return   
		  * @return int <span style="font-family: sans-serif;">(n beyond fnData.size() and n<0 return -1)</span>
		  * @throws
		  */
		 public static int getFnData(int n){
		  if(fnData.size()==0){
		   setFnData();
		  }
		  if(fnData.size()>n&&n>=0){
		   return fnData.get(n);
		  }else{
		   return -1;
		  }
		 }
}

